package com.kingyea.mobilepolice.roleapipermission.controller;

import com.kingyea.mobilepolice.role.bean.Role;
import com.kingyea.mobilepolice.role.service.RoleService;
import com.kingyea.mobilepolice.roleapipermission.bean.RoleApiPermission;
import com.kingyea.mobilepolice.roleapipermission.service.RoleApiPermissionService;
import com.kingyea.mobilepolice.security.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hx
 * @version v1.0.0
 * @description RoleApiPermission 控制层
 * @date Created in 2018-05-15 20:25:07
 */
@RequestMapping(value = "/roleApiPermission")
@RestController
public class RoleApiPermissionController {

    private static final Logger LOGGER = LoggerFactory.getLogger(RoleApiPermissionController.class);

    @Autowired
    private RoleApiPermissionService roleApiPermissionService;
    @Autowired
    private RoleService roleService;

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public List<RoleApiPermission> list() throws Exception {
        return roleApiPermissionService.search(null);
    }

    /**
     * 通过ID查找对象
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/{roleId}", method = RequestMethod.GET)
    public RoleApiPermission detail(@PathVariable Integer roleId) throws Exception {
        return roleApiPermissionService.getById(roleId);
    }

    /**
     * 创建
     *
     * @param roleApiPermission
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public RoleApiPermission create(@RequestBody RoleApiPermission roleApiPermission) throws Exception {
        return roleApiPermissionService.save(roleApiPermission);
    }

    /**
     * 更新
     *
     * @param roleApiPermission
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public RoleApiPermission update(@RequestBody RoleApiPermission roleApiPermission) throws Exception {
        return roleApiPermissionService.update(roleApiPermission);
    }

    /**
     * 删除
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/{roleId}", method = RequestMethod.DELETE)
    public int delete(@PathVariable Integer roleId) throws Exception {
        return roleApiPermissionService.deleteById(roleId);
    }

    @RequestMapping(value = "/searchByRole", method = RequestMethod.POST)
    public Object searchByRole(@RequestBody RoleApiPermission roleApiPermission) throws Exception {
        List<Role> roles = roleService.searchByUserId(SecurityUtils.getCurrentUser().getUserId());
        roleApiPermission.setRoleId((roles == null || roles.size() <= 0 ? null : roles.get(0).getId()));
        return roleApiPermissionService.searchByRole(roleApiPermission);
    }

}
